<?php
/**
 * db.php
 *
 * @copyright 2018 opencart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author StifferChen <chenlin@opencart.cn>
 * @created 2018-05-02 17:36
 * @modified 2018-05-02 17:36
 */

class ModelCommonDb extends Model
{
    public function modifyTableCustomerLogin()
    {
        $field = $this->db->query("describe " . DB_PREFIX . "customer_login account")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "customer_login add account varchar(64) not null default '' after email");
        }
    }

    public function modifyTableCustomer()
    {
        // Add avatar for customer
        $field = $this->db->query("describe " . DB_PREFIX . "customer avatar")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "customer add avatar varchar(120) not null default '' after password");
        }
    }

    public function modifyTableCart()
    {
        $field = $this->db->query("describe " . DB_PREFIX . "cart selected")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "cart add selected tinyint(1) unsigned not null default 1");
        }
    }

    public function modifyTableAddress()
    {
        $field = $this->db->query("describe " . DB_PREFIX . "address city_id")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "address add city_id int unsigned not null default 0");
        }

        $field = $this->db->query("describe " . DB_PREFIX . "address county_id")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "address add county_id int unsigned not null default 0");
        }

        $field = $this->db->query("describe " . DB_PREFIX . "address telephone")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "address add telephone varchar(32) not null default ''");
        }
    }

    public function modifyTableOrder()
    {
        $field = $this->db->query("describe " . DB_PREFIX . "order shipping_city_id")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "order add shipping_city_id int unsigned not null default 0");
        }

        $field = $this->db->query("describe " . DB_PREFIX . "order shipping_county")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "order add shipping_county varchar(120) not null default ''");
        }

        $field = $this->db->query("describe " . DB_PREFIX . "order shipping_county_id")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "order add shipping_county_id int unsigned not null default 0");
        }

        $field = $this->db->query("describe " . DB_PREFIX . "order shipping_telephone")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "order add shipping_telephone varchar(64) not null default ''");
        }
    }

    // 商品评论
    public function createTableProductComments()
    {
        $this->db->query("
            create table if not exists " . DB_PREFIX . "product_comments(
                id int unsigned not null primary key auto_increment,
                customer_id int unsigned not null default 0,
                customer_name varchar(120) not null default '',
                order_id int unsigned not null default 0,
                product_id int unsigned not null default 0,
                product_name varchar(240) not null default '',
                product_model varchar(240) not null default '',
                rating tinyint unsigned not null default 0,
                comments text not null default '',
                date_created timestamp not null default current_timestamp,
                date_modified timestamp not null default current_timestamp,
                status tinyint(1) unsigned not null default 0
            )engine=MyISAM default charset utf8;
        ");

        $this->db->query("
            create table if not exists " . DB_PREFIX . "product_comments_image(
                id int unsigned not null primary key auto_increment,
                comments_id int unsigned not null default 0,
                image varchar(240) not null default ''
            )engine=MyISAM default charset utf8;
        ");
    }

    public function createTableKeywords()
    {
        $this->db->query("
            create table if not exists " . DB_PREFIX . "keywords(
                id int unsigned not null primary key auto_increment,
                link varchar(240) not null default '',
                device tinyint(1) not null default 0,
                sort_order int unsigned not null default 0,
                status tinyint(1) not null default 0
            )engine=MyISAM default charset utf8;
        ");

        $this->db->query("
            create table if not exists " . DB_PREFIX . "keywords_description(
                id int unsigned not null primary key auto_increment,
                keywords_id int unsigned not null default 0,
                language_id int unsigned not null default 0,
                keyword varchar(240) not null default ''
            )engine=MyISAM default charset utf8;
        ");
    }

    public function modifyTableProduct()
    {
        $field = $this->db->query("describe " . DB_PREFIX . "product recommend")->row;
        if (!$field) {
            $this->db->query("alter table " . DB_PREFIX . "product add recommend tinyint(1) not null default 0");
        }
    }

    public function createTableMenu()
    {
        $this->db->query("
            create table if not exists " . DB_PREFIX . "menu(
                menu_id int unsigned not null primary key auto_increment,
                sort_order int unsigned not null default 0,
                href varchar(255) not null default '',
                status tinyint(1) not null default 0
            )engine=MyISAM default charset utf8;
        ");

        $this->db->query("
            create table if not exists " . DB_PREFIX . "menu_description(
                menu_description_id int unsigned not null primary key auto_increment,
                menu_id int unsigned not null default 0,
                language_id int unsigned not null default 0,
                title varchar(120) not null default ''
            )engine=MyISAM default charset utf8;
        ");
    }
}